<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
	require '../../include/paging.inc.php';
	
	
	if(isset($_POST['is_submitted'])){
		
		if(isset($_FILES['calls'])){
			price_update('calls');
		}elseif(isset($_FILES['fax'])){
			price_update('fax');
		}elseif(isset($_FILES['sms'])){
			price_update('sms');
		}elseif(isset($_FILES['conference'])){
			price_update('conference');
		}else{
			
		}
		
		
	}
	
	function price_update($type){
		if (is_uploaded_file($_FILES[$type]['tmp_name'])) {
			
			if (($handle = fopen($_FILES[$type]['tmp_name'], "r")) !== FALSE) {
						
						$sql = "DELETE FROM `price_list` WHERE `category` = '".$type."'";
						query($sql);
						
						while (($row = fgetcsv($handle, 1000, ";")) !== FALSE) {
							
							if($row[2] != 'SELL'){
							
								$sql = "INSERT INTO price_list (category, country, price)
										VALUES ( '".$type."','".$row[1]."','". str_replace(',','.',$row[2])."')";
								query($sql);
							}
						}
						
						$_SESSION['message'] = 'Price list '.$type.' update successfully!';
			}
		}
		
		redirect('price.php');	
	}
		
		head_admin("Price list");
	?>
			
				<?php if ($_SESSION['message']) { ?>
				<div class="message green">
					<?=$_SESSION['message']?>
				</div>
                <?php if ($error_message) { ?>
                    <div class="message red">
                        <h3>Error!</h3>
                        <?=$error_message?>
                    </div>
                <?php } ?>
				<?php unset($_SESSION['message']); } ?>
			
			
				
				<h3>Price List Import</h3>
				
				<form id="calls_form" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="is_submitted" value="1" />
                <span style="float:left;">
                <label  for="calls">Calls price list (CSV):</label><br />
                <input class="inputbox" type="file" name="calls" accept=".csv"/>
                </span><br />
                <input id="submit" type="submit" value="update" />
                </form>	
                
                <br /><br /> <br /> <br />  <br /><br />  
                
                <form id="fax_form" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="is_submitted" value="1" />
                <span style="float:left;">
                <label  for="fax">FAX price list (CSV):</label><br />

                <input class="inputbox" type="file" name="fax" accept=".csv"/>
                </span><br />
                <input id="submit" type="submit" value="update" />
                </form>	
                
                <br /><br /> <br /><br />   <br /><br /> 
                
                <form id="sms_form" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="is_submitted" value="1" />
                <span style="float:left;">
                <label  for="sms">SMS price list (CSV):</label><br />
                <input class="inputbox" type="file" name="sms" accept=".csv"/>
                </span><br />
                <input id="submit" type="submit" value="update" />
                </form>	
                
                <br /><br /> <br /><br />   <br /><br />  
                
                <form id="conference_form" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="is_submitted" value="1" />
                <span style="float:left;">
                <label  for="conference">Conference price list (CSV):</label><br />
                <input class="inputbox" type="file" name="conference" accept=".csv"/>
                </span><br />
                <input id="submit" type="submit" value="update" />
                </form>	
                
                <br /><br /> <br /><br /><br /><br /> <br /><br />
                
                
                
            
				
                
            
<?php
	foot_admin(); 
?>